import React from "react" // React.isValidElement
import {Platform, Dimensions, PixelRatio} from "react-native"
const {width, height} = Dimensions.get("window") //强制竖屏
const pxRatio = PixelRatio.get();
const w = PixelRatio.getPixelSizeForLayoutSize(width);
const h = PixelRatio.getPixelSizeForLayoutSize(height);
const designSize = {width:375, height:667};
const sc = designSize.width / w;
const scale = (1 / sc) / pxRatio;
// const iphoneXAspect = parseFloat((height / width).toString().substring(0, 5));
// && (iphoneXAspect === 2.165 || iphoneXAspect === 0.461),
const isIphoneX = Platform.OS === 'ios' && ((width == 375 && height == 812) || (width == 414 && height == 896));
export const Jpp = Object.freeze({
    pxRatio,
    scale,
    width, //: designSize.width,
    height, //: parseFloat((h * sc).toFixed(2)),
    isAndroid : (Platform.OS === "android"),
    isIOS : (Platform.OS === "ios"),
    // 竖屏
    isIphoneX,
    navHeight : isIphoneX ? 72 : 48,
    inputHeight : 40,
    gameName:"券皇",
    questionMark:"???",
    dateTemp : 1555555555555,
    imageTemp : "",
});

// 常用颜色
export const Color = Object.freeze({
    // 主题色
    theme : "#F5436D", 
    inverseTheme : "white",
    line : "#E7E7E7",
    back : "#F7F7F7",
    inverseBack : "#333",
    c3 : "#333",
    c6 : "#666",
    c9 : "#999",
    transparent : "transparent",
    whiteOpacity : "rgba(255,255,255,0.5)"
});
const transform = [
    {translateX : -Jpp.width * 0.5},
    {translateY : -Jpp.height * 0.5},
    {scaleX:scale},
    {scaleY:scale},
    {translateX : Jpp.width * 0.5},
    {translateY : Jpp.height * 0.5},
];
function getLayoutForEval(pad, margin){
return "var padding="+pad+
",paddingLeft="+pad+
",paddingRight="+pad+
",paddingTop="+pad+
",paddingBottom="+pad+
",paddingHorizontal="+pad+
",paddingVertical="+pad+
",margin="+margin+
",marginLeft="+margin+
",marginRight="+margin+
",marginTop="+margin+
",marginBottom="+margin+
",marginHorizontal="+margin+
",marginVertical="+margin+
";";
}
// Layout 布局 常用字号, 边距
export const Lt = Object.freeze({
    width, height,
    c12 : 12,
    c13 : 13,
    c14 : 14,  // 默认 14
    c16 : 16,
    c18 : 18,
    c20 : 20,
    pad : 10,
    margin : 10,
    radius : 8,
    radiu2 : 16,
    resolution : Object.freeze({width:Jpp.width, height:Jpp.height,transform}),
    eval : getLayoutForEval(10, 10),
});

// 常用函数
export class Jx {
    static _increment = 0;
    static toPx(dp){return PixelRatio.getPixelSizeForLayoutSize(dp);}
    static toDp(pt){return PixelRatio.roundToNearestPixel(pt);}
    static toVertical(v){return Jpp.isAndroid ? (v/2) : v;}
    static getIncrement(){return ++this._increment;}
    static isElement(any){return React.isValidElement(any);}
    static isString(any){return typeof(any) === "string";}
    static isNumber(any){return typeof(any) === "number";}
    static isObject(any){return typeof(any) === "object";}
    static isObjectEmpty(any){return typeof(any) === "object" && JSON.stringify(any) === "{}"}
    static isBoolean(any){return typeof(any) === "boolean";}
    static isUndefined(any){return typeof(any) === "undefined";}
    static isArray(arr){return Array.isArray(arr);}
    static isArrayEmpty(arr){return Array.isArray(arr) && arr.length === 0;}
    static isFloat(str){ return /^[0-9]+.?[0-9]*$/.test(str);}
    // 保留数字
    static replaceInt(str){return str.replace(/[^\d]/g, "");}
    // 保留汉字
    static replaceChinese(str){return str.replace(/[^\u4e00-\u9fa5]/g, "");}
    // 保留数字字母
    static replaceDAZ(str){return str.replace(/[^\a-\z\A-\Z0-9]/g, "");}
    static replaceTrim(str){return str.replace(/^\s+|\s+$/gm,'');}
    static strLen(str) {return str.replace(/[\u0391-\uFFE5]/g, "aa").length;} //先替换中文
    static stringify(text){return this.isObject(text) ? JSON.stringify(text) : text;}

    static toChildren(props){
        let {title, children} = props;
        if(this.isUndefined(children)){
            children = (title || "");
        }
        return children;
    }
    
    static toDate(ti, symbol, cut_link) {
        symbol = symbol || "-";
        let date = new Date(ti);
        ti = n=>(n>9)?n:("0"+n);
        symbol = date.getFullYear() + symbol + ti(date.getMonth()+1) + symbol + ti(date.getDate());
        if(this.isBoolean(cut_link)){
            return symbol;
        }
        cut_link = cut_link || " ";
        return symbol + cut_link + date.toTimeString().substr(0, 8)
    }

    static toUri(photo){
        return photo.enableBase64 ? photo.base64 : photo.uri;
    }

    static toFile(uri){
        let arr = uri.split('/');
        let starIndex = uri.lastIndexOf(".");
        let suffix = uri.substring(starIndex,uri.length).toLocaleLowerCase();
        if(suffix === ".jpg" || suffix===".png" || suffix === ".jpeg"){
            return {
                uri: uri, 
                type: "application/octet-stream", 
                name: arr[arr.length - 1] 
            };
        }
        return null;
    }



    static toPost(obj, images, token){
        let body = null;
        let method ='POST';
        if(obj && obj.method){
            if(obj.method === 'GET'){
                method = 'GET';
                delete obj.method;
            }
        }
        if(this.isString(obj)){
            body = obj;
            // headers['Content-Type'] = 'application/json';
        } else if((obj && !this.isObjectEmpty(obj)) || images){
            body = new FormData();
            if(Jx.isArray(images) && images.length > 0){
                let file;
                for (const image in images) {
                    file = this.toFile(this.toUri(image));
                    if(uri){
                        body.append("file", file);
                    }
                }           
            }
            for (const key in obj) {
                body.append(key,obj[key]);
            }
        }
        let headers = {
            // 'Accept': 'application/json',
            // 'Content-Type': 'application/json',
            // 'charset':'UTF-8'
        };
        if(token){
            headers.token = token;
        }
        if(body){
            return {
                method,
                headers,
                body,
            };
        }
        return {
            method,
            headers,
        };
    }

    static checkInput(str){
        if(str.length < 1){
            return "输入无效";
        }
        return ""
    }

    static checkInterger(str){
        let str2 = this.checkInput(str);
        if(str2){
            return str2;
        }
        str2 = parseInt(str);
        if(str2 < 1 || str2.toString() != str){
            return "输入无效";
        }
        return ""
    }
}